package com.example.anew.journey.mynewjourney.firstchangelogic.firstChangeFeedBack.persenter;

import android.content.Context;
import android.text.TextUtils;

import com.example.anew.journey.mynewjourney.base.MyApplication;
import com.example.anew.journey.mynewjourney.bean.TakeDateBean;
import com.example.anew.journey.mynewjourney.config.RBConstants;
import com.example.anew.journey.mynewjourney.firstchangelogic.firstChangeFeedBack.bean.FeedBackBean;
import com.example.anew.journey.mynewjourney.firstchangelogic.firstChangeFeedBack.model.FirstChangeFeedBackModel;
import com.example.anew.journey.mynewjourney.firstchangelogic.firstChangeFeedBack.model.ISFirstChangeFeedBackModel;
import com.example.anew.journey.mynewjourney.firstchangelogic.firstChangeFeedBack.view.ISFirstChangeFeedBackView;
import com.google.gson.Gson;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.Callback;

import okhttp3.Call;
import okhttp3.Response;

/**
 * 杭州惟君医疗科技有限公司10
 * Created by 20433 on 2018/3/26
 *
 * @desc 东风山河绿，春已不折柳，繁花看不尽，处处惹人留
 * @desc 佛祖镇楼，bug辟容易
 */

public class FirstChangeFeedBackPersenter {
    private ISFirstChangeFeedBackView mISFirstChangeFeedBackView ;
    private ISFirstChangeFeedBackModel mISFirstChangeFeedBackModel ;

    public FirstChangeFeedBackPersenter(ISFirstChangeFeedBackView miSFirstChangeFeedBackView) {
        this.mISFirstChangeFeedBackView = miSFirstChangeFeedBackView;
        mISFirstChangeFeedBackModel =new FirstChangeFeedBackModel();

    }

    public void onClickSend(Context mContext){
       String str =  mISFirstChangeFeedBackView.getInputContext();
        if(TextUtils.isEmpty(str)) mISFirstChangeFeedBackView.showToastInformation("请输入您宝贵的意见");
        TakeDateBean bean = mISFirstChangeFeedBackModel.TakeDate(mContext);
        submitFeedBack(str,bean.getUser_id());
    }

    private void submitFeedBack(String str, String user_id) {
        String url = RBConstants.SERVER_URL+RBConstants.FEED_BACK;
        OkHttpUtils
                .post()
                .url(url)
                .addParams("cus_id",user_id)
                .addParams("feedback", str)
                .build()
                .execute(new Callback() {
                    @Override
                    public Object parseNetworkResponse(Response response, int id) throws Exception {
                        String NetworkResponseDate = response.body().string();
                        Gson gson = new Gson();
                      //  NLogger.e("TAG","============NetworkResponseDate================="+NetworkResponseDate);
                        final FeedBackBean feedBackBean =   gson.fromJson(NetworkResponseDate, FeedBackBean.class);
                        MyApplication.getmHandler().post(new Runnable() {
                            @Override
                            public void run() {
                                if(feedBackBean.errorCode == 200){
                                    mISFirstChangeFeedBackView.showToastInformation("反馈成功");
                                    mISFirstChangeFeedBackView.finshPager();
                                }else{
                                    mISFirstChangeFeedBackView.showToastInformation("反馈失败");
                                }
                            }
                        });
                        return null;
                    }

                    @Override
                    public void onError(Call call, Exception e, int id) {
                        //NLogger.e("TAG","============e================="+e);
                      //  mISFirstChangeFeedBackView.showToastInformation("请查看网络");
                    }

                    @Override
                    public void onResponse(Object response, int id) {

                    }
                });
    }
}
